from core.memcache import CacheManager
from constants import Company_id
from daos import companyDao,projectDao

class BaseCacheDao:
    
    def dummyCallback(self,result):
        print result

class CompanyCacheDao(BaseCacheDao):
    
    def __init__(self,companyDao):
        self.companyDao = companyDao
    
    def getCompany(self,process):
        key = "company_" + str(Company_id)
        def callback(company):
            if not company :
                company = self.companyDao.getCompany()
                CacheManager.getInstance().store(key,company,self.dummyCallback)
            process(company)
        CacheManager.getInstance().fetch(key,callback)
    
    def saveCompanyInfo(self,name,intro,process):
        company = self.companyDao.saveCompanyInfo(name, intro)
        key = "company_" + str(company.id)
        CacheManager.getInstance().store(key,company,self.dummyCallback)
        process(company)

class ProjectCacheDao(BaseCacheDao):
    
    def __init__(self,projectDao):
        self.projectDao = projectDao

    def getAllProject(self,companyId,process):
        key = "projectids_" + str(companyId)
        def callback(projectIds):
            projects = []
            if not projectIds:
                projects = self.projectDao.getAllProject(companyId)
                projectIds = []
                for project in projects :
                    pkey = "project_" + str(project.id)
                    CacheManager.getInstance().store(pkey,project,self.dummyCallback)
                    projectIds.append(project.id)
                CacheManager.getInstance().store(key,projectIds,self.dummyCallback)
                process(projects)
            else:
                def merge(project):
                    projects.append(project)
                    process(projects)
                for projectId in projectIds :
                    self.getProject(projectId, merge)
        CacheManager.getInstance().fetch(key,callback)

    def getProject(self,projectId,process):
        key = "project_" + str(projectId)
        def callback(project):
            if not project:
                project = self.projectDao.getProject(projectId)
                if project :
                    CacheManager.getInstance().store(key,project,self.dummyCallback)
            process(project)
        CacheManager.getInstance().fetch(key,callback)

    def removeProject(self,projectId,process):
        
        pass

    def addProject(self,name,intro,company,process):
        
        pass

class EmployeeCacheDao(BaseCacheDao):

    def getAllEmployee(self):
        pass

    def getProjectEmployee(self,projectId):
        pass

    def getEmployee(self,employeeId):
        pass

    def removeEmployee(self,employeeId):
        pass

    def addEmployee(self,name,gender,project):
        pass

    def changeProject(self,employeeId,project):
        pass

class DeliveryCacheDao(BaseCacheDao):
    
    def getAllDeliveries(self):
        pass
    
    def addDelivery(self,name,intro,address,phone):
        pass
    
    def getDelivery(self,deliveryId):
        pass
    
    def removeDelivery(self,deliveryId):
        pass
    
class DishCacheDao(BaseCacheDao):
    
    def getDeliveryDishes(self,deliveryId):
        pass
    
    def getDish(self,dishId):
        pass
    
    def removeDish(self,dishId):
        pass
    
    def addDish(self,name,price,delivery):
        pass

class OrderItemCacheDao(BaseCacheDao):

    def addOrderItem(self,employeeId,dishId,ip,orderTime):
        pass

    def removeOrderItem(self,orderItemId):
        pass

    def getOrderItem(self,orderItemId):
        pass

    def getAllOrderItems(self):
        pass

    def getProjectOrderItems(self,projectId):
        pass

    def getEmployeeOrderItem(self,employeeId):
        pass

companyCacheDao = CompanyCacheDao(companyDao)
projectCacheDao = ProjectCacheDao(projectDao)